﻿package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class IdScreen extends Sprite{
		public var completed:Boolean;
		private var delayInstructionTimer:Timer;
		private var readyToContinue:Boolean = false;
		private var getUserID:Boolean;
		private var clickSquare:MyButton;
		//tFields holds the instructions, utFields holds the interactive user textfields
		private var instructs:Array, instructsCharLimits:Array, tfields:Array, utfields:Array;		
		private var introTField:TextField, instrClickTField:TextField;
		private var introText:String;
		
		public function get text1():String{if(utfields.length>0){return utfields[0].text}else{return ""}}
		public function get text2():String{if(utfields.length>1){return utfields[1].text}else{return ""}}
		public function get text3():String{if(utfields.length>2){return utfields[2].text}else{return ""}}
		public function get text4():String{if(utfields.length>3){return utfields[3].text}else{return ""}}
		
		public function IdScreen(introText:String, instructs1:String, instructs1CharLimit:int=0, instructs2:String="", instructs2CharLimit:int=0, instructs3:String="", instructs3CharLimit:int=0, instructs4:String="", instructs4CharLimit:int=0)
		{
			this.introText = introText;
			instructs = new Array(instructs1, instructs2, instructs3, instructs4);
			instructsCharLimits = new Array(instructs1CharLimit, instructs2CharLimit, instructs3CharLimit, instructs4CharLimit);
			this.addEventListener(Event.ADDED, handleAdded);
		} 
		public function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			var tfield:TextField, utfield:TextField;
			this.graphics.drawRect(0,0,this.parent.width, this.parent.height);
			trace("idscreen: this.parent.width", this.parent.width,"this.parent.height",this.parent.height); 
			// Place an intro TextField at top of screen centered
			if (introText.length > 0){
				introTField = new TextField();
				introTField.defaultTextFormat = new TextFormat("Arial", 20,0x000000);
				introTField.multiline = true;
				introTField.width = this.width-40;
				introTField.text = introText
				introTField.autoSize = "left";
				introTField.selectable = false;
				introTField.x = 20;
				introTField.y = 40;
				this.addChild(introTField);
			}
			
			tfields = new Array();
			utfields = new Array();
			
			this.graphics.drawRect(0,0,parent.width,parent.height);
			for (var i:int=0; i < instructs.length; i++){
				if (instructs[i].length > 0){
					tfield = new TextField();
					tfield.defaultTextFormat = new TextFormat("Arial", 20,0x000000);
					tfield.multiline = true;
					tfield.text = instructs[i];
					tfield.autoSize = "left";
					tfield.selectable = false;
					tfield.x = 20;
					tfield.y = i*100 + 100;
					this.addChild(tfield);
					tfields.push(tfield);
					
					utfield = new TextField();
					utfield.defaultTextFormat = new TextFormat("Arial", 20,0x000000);
					utfield.multiline = false;
					utfield.type = "input";
					utfield.backgroundColor = 0xFFFFFF;
					utfield.border = true;
					utfield.width = this.width/4*3;
					utfield.height = tfield.height;
					utfield.text = "";
					if (instructsCharLimits[i]>0)
					{
						utfield.maxChars = instructsCharLimits[i];
						utfield.width = int(utfield.defaultTextFormat.size) * instructsCharLimits[i] * 1.5;
					}
					utfield.x = 20;
					utfield.y = tfield.y + tfield.height + 20;
					this.addChild(utfield);
					utfields.push(utfield);
				} else {
					break;
				}
			}
			
			
			// Three second delay on click instructions
			delayInstructionTimer = new Timer(3000);
			delayInstructionTimer.addEventListener(TimerEvent.TIMER, delayInstruction);
			delayInstructionTimer.start();
			
			
			instrClickTField = new TextField();
			instrClickTField.defaultTextFormat = new TextFormat("Arial", 24,0x000000);
			instrClickTField.multiline = true;
			instrClickTField.text = "Click on the square to continue.";
			instrClickTField.autoSize = "left";
			instrClickTField.x = 20;
			instrClickTField.y = utfield.y + utfield.height + 40;
			
			clickSquare = new MyButton(40,40,0xDDDDDD,"");
			clickSquare.y = instrClickTField.y + instrClickTField.height + 20;
			clickSquare.x = 20;
			
			
		}
		
		/* Occurs after a delay to instruct user to press red square to continue */
		private function delayInstruction(event:TimerEvent):void
		{
			delayInstructionTimer.stop();
			delayInstructionTimer.removeEventListener(TimerEvent.TIMER, delayInstruction);
			this.addChild(instrClickTField);
			this.addChild(clickSquare);
			clickSquare.addEventListener(Event.COMPLETE, mousePressHandler);
			readyToContinue = true;
		}
		
		/* When user presses the button we must check whether the userID is filled out and the labels contain either even or odd */
		private function mousePressHandler(event:Event):void
		{
			if (readyToContinue){//delay 
				// make sure that each textbox includes text
				completed = true;
				for (var i:int=0; i < utfields.length; i++){
					if (utfields[i].text.length <= 0){
						completed = false;
						instrClickTField.text = "You haven't completed all fields.";
						break;
					}
				}
				if (completed){
					clickSquare.removeEventListener(MouseEvent.CLICK, mousePressHandler);
					this.dispatchEvent(new Event(Event.COMPLETE));
				}				
			}
		}
	}
}